package com.yc.service.xcx.base;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import me.chanjar.weixin.mp.bean.result.WxMpUser;

@Service
public class XCXBaseService implements IXCXBaseService {

	protected Logger logger = LoggerFactory.getLogger(getClass());
	   
	@Override
	public String requestGet(String urlWithParams) throws IOException {
	      CloseableHttpClient httpclient = HttpClientBuilder.create().build();
	        HttpGet httpget = new HttpGet(urlWithParams);
	        httpget.addHeader("Content-Type", "text/html;charset=UTF-8");
	
	        CloseableHttpResponse response = httpclient.execute(httpget);
	        System.out.println("StatusCode -> " + response.getStatusLine().getStatusCode());

	        HttpEntity entity = response.getEntity();
	        String jsonStr = EntityUtils.toString(entity);
	        System.out.println(jsonStr);
	        
	        httpget.releaseConnection();
	        return jsonStr;
	}

	@Override
	public String requestPost(String url, List<NameValuePair> params) throws ClientProtocolException, IOException {
		 CloseableHttpClient httpclient = HttpClientBuilder.create().build();

	        HttpPost httppost = new HttpPost(url);
	        httppost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));

	        CloseableHttpResponse response = httpclient.execute(httppost);
	        System.out.println(response.toString());

	        HttpEntity entity = response.getEntity();
	        String jsonStr = EntityUtils.toString(entity, "utf-8");
	        System.out.println(jsonStr);

	        httppost.releaseConnection();
	        return jsonStr;
	}
	
	
	@Override
	public String requestGetJson(String urlWithParams) throws IOException {
	      CloseableHttpClient httpclient = HttpClientBuilder.create().build();
	        HttpGet httpget = new HttpGet(urlWithParams);
	        httpget.addHeader("Content-Type", "'application/json';charset=UTF-8");
	        //配置请求的超时设置
	        //RequestConfig requestConfig = RequestConfig.custom()
	       //     .setConnectionRequestTimeout(50)
	        //    .setConnectTimeout(50)
	        //    .setSocketTimeout(50).build();
	       //httpget.setConfig(requestConfig);

	        CloseableHttpResponse response = httpclient.execute(httpget);
	        System.out.println("StatusCode -> " + response.getStatusLine().getStatusCode());

	        HttpEntity entity = response.getEntity();
	        String jsonStr = EntityUtils.toString(entity);
	        System.out.println(jsonStr);
	        
	        httpget.releaseConnection();
	        return jsonStr;
	}



}
